<template>
  <div class="user-list">
    <!-- 面包屑 -->
    <el-row>
      <my-breadcrumb level1="商品管理" level2="商品分类"></my-breadcrumb>
    </el-row>
    <!-- 搜索 -->
    <el-row :gutter="20">
      <el-button
        type="success"
        icon="el-icon-circle-plus-outline"
        @click="dialogVisibleAdd = true"
        >添加分类</el-button
      >
    </el-row>
    <!-- 表格 -->
    <el-row>
      <el-table :data="tableData" style="width: 100%" row-key="cat_id">
        <el-table-column
          prop="cat_name"
          label="分类名称"
          width="160"
        ></el-table-column>
        <el-table-column prop="cat_level" label="级别" width="160">
          <template slot-scope="scope">
            {{ scope.row.cat_level | fmLevelToText }}
          </template>
        </el-table-column>
        <el-table-column prop="cat_deleted" label="是否有效" width="160">
          <template slot-scope="scope">
            {{ scope.row.cat_deleted ? '无效' : '有效' }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <el-row>
              <el-tooltip effect="dark" content="编辑" placement="top">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  plain
                  size="mini"
                  @click="handleEditCategory(scope.row)"
                ></el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="删除" placement="top">
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  plain
                  size="mini"
                  @click="handleDeleteCategory(scope.row)"
                ></el-button>
              </el-tooltip>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <!-- 分页 -->
    <el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-row>
    <!-- 添加用户 -->
    <category-add
      :dialogVisibleAdd="dialogVisibleAdd"
      @addCategoryClosed="dialogVisibleAdd = false"
      @addCategorySuccess="getCategoryListData"
    ></category-add>
    <!-- 编辑用户 -->
    <category-edit
      :dialogVisibleEdit="dialogVisibleEdit"
      :categoryInfo="currCategoryInfo"
      @editCategoryClosed="dialogVisibleEdit = false"
      @editCategorySuccess="getCategoryListData"
    ></category-edit>
  </div>
</template>

<script>
import MyBreadcrumb from '@/components/MyBreadcrumb'
import CategoryAdd from '@/views/goods/goods-category/components/CategoryAdd'
import CategoryEdit from '@/views/goods/goods-category/components/CategoryEdit'

export default {
  components: {
    MyBreadcrumb,
    // ElTreeGrid,
    CategoryAdd,
    CategoryEdit
  },
  data() {
    return {
      query: '',
      tableData: [],
      total: 0,
      pageSize: 5,
      pageNum: 1,
      dialogVisibleAdd: false,
      dialogVisibleEdit: false,
      currCategoryInfo: null
    }
  },
  filters: {
    fmLevelToText(val) {
      const obj = {
        0: '一级',
        1: '二级',
        2: '三级',
        3: '四级'
      }
      return obj[val]
    }
  },
  created() {
    this.getCategoryListData()
  },
  computed: {},
  methods: {
    async getCategoryListData() {
      const { data } = await this.$http.get('/categories', {
        params: {
          query: this.query,
          pagenum: this.pageNum,
          pagesize: this.pageSize
        }
      })

      // console.log(data)
      if (data.meta.status === 200) {
        const { total, result } = data.data
        this.tableData = result
        console.log(result)
        this.total = total
      } else {
        this.$message.warning(data.meta.msg)
      }
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val
      // 切换每页数据条目数量的时候，页码切回到 1
      this.pageNum = 1
      this.getCategoryListData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.pageNum = val
      this.getCategoryListData()
    },

    // 删除按钮
    handleDeleteCategory(categoryInfo) {
      console.log(categoryInfo)
      this.$confirm('此操作将删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const {
            data: { meta }
          } = await this.$http.delete('/categories/' + categoryInfo.cat_id)

          if (meta.status === 200) {
            this.$message.success(meta.msg)
            this.getCategoryListData()
          } else {
            this.$message.error(meta.msg)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 编辑用户
    handleEditCategory(categoryInfo) {
      this.dialogVisibleEdit = true
      this.currCategoryInfo = JSON.parse(JSON.stringify(categoryInfo))
    },
    addCategorySuccess() {
      this.getCategoryListData()
    }
  }
}
</script>

<style lang="less" scoped>
.user-list {
  .el-row {
    padding: 5px 0;
  }
}
</style>
